import 'package:flutter/material.dart';
import "package:pull_to_refresh/pull_to_refresh.dart";

class LoadMorePage extends StatefulWidget {
  final String title;

  LoadMorePage({Key key, this.title}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return LoadMorePageState();
  }
}

class LoadMorePageState extends State<LoadMorePage> {
  List<String> list = List();
  bool isFinish = false;
  RefreshController _refreshController = new RefreshController();

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: Container(
        child: SmartRefresher(
            enablePullDown: true,
            enablePullUp: true,
            onRefresh: _onRefresh,headerBuilder: _buildHeader,footerBuilder: _buildFooter,
            controller: _refreshController,
            onOffsetChange: _onOffsetCallback,
            child: new ListView.builder(
              itemExtent: 40.0,
              itemCount: list.length,
              itemBuilder: (context, index) {
                return Text('${list[index]}');
              },
            )),
      ),
    );
  }
  Widget _buildHeader(context,mode){
    return new ClassicIndicator(mode: mode,refreshingText: '刷新中...',noDataText: '没有啦！',completeText: '刷新完成',releaseText: '释放时刷新',idleText: '下拉以刷新',failedText: '刷新失败',);
  }


  Widget _buildFooter(context,mode){
    return new ClassicIndicator(mode: mode,idleIcon: Icon(Icons.arrow_upward),refreshingText: '刷新中...',noDataText: '没有啦！',completeText: '刷新完成',releaseText: '释放时刷新',idleText: '上拉以刷新',failedText: '刷新失败',);
  }
  void _onRefresh(bool up) {
    if (up) {
      //headerIndicator callback
      new Future.delayed(const Duration(milliseconds: 2009)).then((val) {
        print('===下拉');
        _refreshController.sendBack(true, RefreshStatus.completed);
        setState(() {
          this.list.removeLast();
        });
      });
    } else {
      new Future.delayed(const Duration(milliseconds: 2009)).then((val) {
      print('===上拉');
      _refreshController.sendBack(false, RefreshStatus.canRefresh);
      load();
    });
    }
  }

  Future<bool> _loadMore() async {
    print("onLoadMore");
    await Future.delayed(Duration(seconds: 2, milliseconds: 100));
    load();
    return true;
  }

  void load() {
    setState(() {
      for (int i = 0; i < 20; i++) {
        list.add('item==>${i}');
      }
      isFinish = false;
    });
  }

  void _onOffsetCallback(bool up, double offset) {}
}
